﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using BUS;
using System.Diagnostics;
using System.Data;

namespace QLDB
{
    public partial class frmSanVD : Form
    {
        public frmSanVD()
        {
            InitializeComponent();
        }
        public String Tinh = "";
        public String SVD = "";
        //hien thi
        void hienthi()
        {
            DataTable tam = SanVD_BUS.ListSo();
            dgvSanVD.DataSource = tam;

        }
        //from load
        //anhien textbox
        private void anhientexbox(Boolean t)
        {
            txtMaSan.ReadOnly=t;
            txtTenSan.ReadOnly=t;
            txtDiaChi.ReadOnly=t;
        }
        private void frmSanVD_Load(object sender, EventArgs e)
        {
            hienthi();
            anhientexbox(true);
        }
        //datagriview
        private void dgvSanVD_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            btnLuu.Enabled = false;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = true;
            txtMaSan.Text = dgvSanVD.CurrentRow.Cells[0].Value.ToString();
            txtTenSan.Text = dgvSanVD.CurrentRow.Cells[1].Value.ToString();
            txtDiaChi.Text = dgvSanVD.CurrentRow.Cells[2].Value.ToString();
        }
        //luu san van dong
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                SanVD_DTO them = new SanVD_DTO();
                them.MaSan1 = txtMaSan.Text;
                them.TenSan1 = txtTenSan.Text;
                them.DiaChi1 = txtDiaChi.Text;
                SanVD_BUS.Them(them);
                hienthi();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi roi ban oi" + ex.Message.ToString());
            }

        }
        //xoa san van dong
        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                string xMaSan = txtMaSan.Text;
                DialogResult dlg = MessageBox.Show("Ban co chac muon xoa", "Xoa Nhan Vien", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dlg == DialogResult.OK)
                {
                    SanVD_BUS.Xoa(xMaSan);
                    hienthi();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Xoa khong thang cong!");
            }
        }
        //Cap Nhat san van dong
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            //try
            //{
                SanVD_DTO cnsvd = new SanVD_DTO();
                cnsvd.MaSan1 = txtMaSan.Text;
                cnsvd.TenSan1 = txtTenSan.Text;
                cnsvd.DiaChi1 = txtDiaChi.Text;
                SanVD_BUS.CapNhat(cnsvd);
                MessageBox.Show("Cap nhat thanh cong");
                hienthi();
            //}
            //catch
            //{
            //    MessageBox.Show("Cap nhat khong thanh cong!");
            //}
        }
        //them moi svnd
        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            anhientexbox(false);
            btnLuu.Enabled = true;
            btnXoa.Enabled = false;
            btnCapNhat.Enabled = false;
            txtMaSan.Text = "";
            txtTenSan.Text = "";
            txtDiaChi.Text = "";

        }

        private void dgvSanVD_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

       
       

    }
 }

